package com.sdcqjy.property.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.sdcqjy.property.R;
import com.sdcqjy.property.base.BaseService;
import com.sdcqjy.property.wxapi.WChatConstants;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;

/**
 * 分享菜单<br>
 * 更多分享
 */
public class MoreAreaShareMenu extends PopupWindow implements OnClickListener {
    private Activity context;
    private View conentView;
    private View layout1;

    public MoreAreaShareMenu(Activity context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.menu_more_area_share, null);
        // int h = context.getWindowManager().getDefaultDisplay().getHeight();
        // int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        // ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        // this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.AnimationPreview);

        //mTencent = Tencent.createInstance(QQConfig.APP_ID, context);
        initView();
    }

    private void initView() {
        conentView.findViewById(R.id.textShareAction).setOnClickListener(this);// 分享至好友
        conentView.findViewById(R.id.textShareToWeixin)
                .setOnClickListener(this);// 分享至朋友圈
        conentView.findViewById(R.id.textShareToQQ).setOnClickListener(this);
        conentView.findViewById(R.id.textShareToQQZone)
                .setOnClickListener(this);
        conentView.findViewById(R.id.textShareCancel).setOnClickListener(this);// 取消分享
        conentView.findViewById(R.id.view1).setOnClickListener(this);

        layout1 = conentView.findViewById(R.id.layout1);
    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            super.showAsDropDown(parent);
        } else {
            super.dismiss();
        }
    }

    public void onClick(View v) {
        if (!hasData && v.getId() != R.id.textShareCancel) {
            return;
        }
        switch (v.getId()) {
            case R.id.textShareAction:// 分享至好友
                onShareItem(0);
                break;
            case R.id.textShareToWeixin:// 分享至朋友圈
                onShareItem(1);
                break;
            case R.id.textShareToQQ:// 分享到QQ
                //shareToQQ();
                break;
            case R.id.textShareToQQZone:// 分享到QQ空间
                //shareToQzone();
                break;
            case R.id.view1:
            case R.id.textShareCancel:// 取消分享
                break;
        }
        this.dismiss();
    }

    private void onShareItem(int type) {
        wechatShar(type);
    }

    private String title, contentTitle, url, bitmapUrl;
    private Bitmap bitmapShow;
    private ArrayList<String> bitmapUrls;
    //private Tencent mTencent;
    private boolean hasData = false;

    /**
     * <p>
     * Tencent mTencent = Tencent.createInstance(Constants.QQAppId, context);
     * </p>
     *
     * @param contentTitle 内容和标题
     * @param url          访问地址
     * @param bitmapShow
     * @param bitmapUrls
     */
    public void setData(String contentTitle, String url, Bitmap bitmapShow,
                        ArrayList<String> bitmapUrls) {
        String contentStr = "";
        setData(contentTitle, url, bitmapShow, bitmapUrls, contentStr);
    }

    public void setData(String contentTitle, String url, Bitmap bitmapShow,
                        ArrayList<String> bitmapUrls, String contentStr) {
        hasData = true;
        if (contentTitle == null || contentTitle.equals("")) {
            title = " ";
        } else {
            title = contentTitle;
        }
        this.url = url;
        this.contentTitle = contentStr;
        this.bitmapShow = bitmapShow;
        this.bitmapUrls = new ArrayList<>();
        for (String str : bitmapUrls) {
            if (str.contains("://")) {
                this.bitmapUrls.add(str);
            } else
                this.bitmapUrls.add(BaseService.ImgURL + str);
        }
        // this.bitmapUrls = bitmapUrls;
        //bitmapUrl = this.bitmapUrls.get(0);
    }

    // 微信分享
    private void wechatShar(int type) {
        IWXAPI wxApi = WXAPIFactory.createWXAPI(context, WChatConstants.APP_ID);
        wxApi.registerApp(WChatConstants.APP_ID);

        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        if (type == 1) {
            msg.title = contentTitle;
        } else msg.title = title;
        msg.description = contentTitle;
        // 这里替换一张自己工程里的图片资源
        msg.setThumbImage(bitmapShow);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        // req.scene = SendMessageToWX.Req.WXSceneTimeline;
        req.scene = type == 1 ? SendMessageToWX.Req.WXSceneTimeline
                : SendMessageToWX.Req.WXSceneSession;
        wxApi.sendReq(req);
    }

    // QQ分享
//    private void shareToQQ() {
//        Bundle params = new Bundle();
//        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
//                QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
//        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
//        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, contentTitle);
//        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
//        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, bitmapUrl);
//        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "会养车");
//        // params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
//        // QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);// 提示分享到QQ空间
//        mTencent.shareToQQ(context, params, new BaseUiListener());
//    }
    // QQ空间
//    private void shareToQzone() {
//        // 分享类型
//        Bundle params = new Bundle();
//        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,
//                QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
//        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);// 必填
//        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, contentTitle);// 选填
//        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, url);// 必填
//        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, bitmapUrls);
//        mTencent.shareToQzone(context, params, new BaseUiListener());
//    }
    // onActivityResult返回10103
//    class BaseUiListener implements IUiListener {
//
//        public void onCancel() {// 取消
//            // ShowToast.alertShort(context, "分享取消");
//        }
//
//        public void onComplete(Object arg0) {// 成功
//            // ShowToast.alertShort(context, "分享成功");
//        }
//
//        public void onError(UiError arg0) {// 出错
//            // ShowToast.alertShort(context, "分享出错");
//        }
//    }
}
